import request from "./request";

export const goodsService = {
  // 获取商品列表
  async getGoodsList(params) {
    const queryParams = {
      pageIndex: params.pageIndex || 1,
      pageSize: params.pageSize || 10,
    };

    if (params.categoryId) queryParams.categoryId = params.categoryId;
    if (params.keyword) queryParams.keyword = params.keyword;
    if (params.sortBy) queryParams.sortBy = params.sortBy;
    if (params.sortType) queryParams.sortType = params.sortType;

    return await request({
      url: "Good/list",
      method: "GET",
      data: queryParams,
    });
  },

  // 修改获取商品详情接口
  async getGoodsDetail(spuId) {
    return await request({
      url: `Good/detail/${spuId}`,
      method: "GET",
      transform: (res) => {
        if (res.success && res.data) {
          res.data.salePrice = res.data.salePrice / 100;
          if (res.data.linePrice) {
            res.data.linePrice = res.data.linePrice / 100;
          }
          
          if (res.data.skus) {
            res.data.skus = res.data.skus.map(sku => ({
              ...sku,
              salePrice: sku.salePrice / 100,
              linePrice: sku.linePrice / 100
            }));
          }
        }
        return res;
      }
    });
  },

  // 获取商品分类
  async getCategories() {
    return await request({
      url: "Good/categories/list",
      method: "GET",
    });
  },

  // 获取商品评论
  async getGoodsComments(spuId, params) {
    return await request({
      url: `Good/comments/list/${spuId}`,
      method: "GET",
      data: params,
    });
  },

  // 创建商品
  async createGoods(data) {
    return await request({
      url: "Good/create",
      method: "POST",
      data,
    });
  },

  // 更新商品
  async updateGoods(spuId, data) {
    return await request({
      url: `Good/update/${spuId}`,
      method: "PUT",
      data,
    });
  },

  // 删除商品
  async deleteGoods(spuId) {
    return await request({
      url: `Good/delete/${spuId}`,
      method: "DELETE",
    });
  },

  // 模拟收藏检查
  async checkCollect(spuId) {
    return Promise.resolve({ success: true, data: false });
  },

  // 模拟收藏切换
  async toggleCollect(spuId) {
    return Promise.resolve({ success: true });
  }
};
